package com.shangyitong.yygh_order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangyitong.yygh.enums.RefundStatusEnum;
import com.shangyitong.yygh.model.order.PaymentInfo;
import com.shangyitong.yygh.model.order.RefundInfo;
import com.shangyitong.yygh_order.mapper.RefundInfoMapper;
import com.shangyitong.yygh_order.service.RefundInfoService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        RefundInfo refundInfo = baseMapper.selectOne(new QueryWrapper<RefundInfo>()
                .eq("order_id", paymentInfo.getOrderId())
                .eq("payment_type", paymentInfo.getPaymentType()));
        if (refundInfo != null){
            return refundInfo;
        }
        RefundInfo refund = new RefundInfo();
        refund.setCreateTime(new Date());
        refund.setOrderId(paymentInfo.getOrderId());
        refund.setPaymentType(paymentInfo.getPaymentType());
        refund.setOutTradeNo(paymentInfo.getOutTradeNo());
        refund.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        refund.setSubject(paymentInfo.getSubject());
        refund.setTotalAmount(paymentInfo.getTotalAmount());
        baseMapper.insert(refund);
        return refund;
    }
}
